package top.xiaoshuaila.searescue.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.xiaoshuaila.searescue.pojo.Buoy;
import top.xiaoshuaila.searescue.pojo.Tag;


@Component
public class DataDeal {
    @Autowired
    private Buoy buoy;
    @Autowired
    private Tag tag;
    @Autowired
    private SerialPortservice serialPortservice;
    public void STHPoint(){
        String str="$BOAT:001,GO:"+buoy.getGO()+",Longitude:"+tag.getLongitude()+",Latitude:"+tag.getLatitude();
        serialPortservice.send(str);
    }
    public void STHReturns(){
        String str= "$BOAT:001,RTN";
        serialPortservice.send(str);
    }
    public void STHStop(){
        String str= "$BOAT:001,STB";
        serialPortservice.send(str);
    }
    public void STTGetStatus(){
        String str= "$TAG:"+String.format("%03d",tag.getTAG())+",RPT";
        serialPortservice.send(str);
    }
    public void HTSStatus(String str){
        System.out.println("收到救生圈数据:"+str);
        buoy.setTIME(0);
        str="{"+str+"}";
        str = str.replace("{", "{\"");
        str = str.replace(":", "\":\"");
        str = str.replace(",", "\",\"");
        str = str.replace("}", "\"}");
        JSONObject parse = JSONObject.parseObject(str);
        System.out.println(parse+":parse");
        buoy.setLongitude(parse.getDouble("Longitude"));
        buoy.setLatitude(parse.getDouble("Latitude"));
        buoy.setBOAT(parse.getInteger("$BOAT"));
        buoy.setVOLT(parse.getString("VOLT"));
        if(!buoy.getSTATUS().equals("RTN"))
        buoy.setSTATUS( parse.getString("STATUS"));
        buoy.setMOTOR( parse.getString("MOTOR"));
        System.out.println(buoy.toString()+"buoy");
    }
    public void TTSStatus(String str){
        System.out.println("收到标签数据"+":"+str);
        str="{"+str+"}";
        str = str.replace("{", "{\"");
        str = str.replace(":", "\":\"");
        str = str.replace(",", "\",\"");
        str = str.replace("}", "\"}");
        JSONObject parse = JSONObject.parseObject(str);
        System.out.println(parse+":parse");
        tag.setLongitude(parse.getDouble("Longitude"));
        tag.setLatitude(parse.getDouble("Latitude"));
        tag.setTAG(parse.getInteger("$TAG"));
        tag.setSOG(parse.getString("SOG"));
        tag.setDIS( parse.getString("DIS"));
        System.out.println(tag.toString()+"tag");
    }
    public void other(String str){
        System.out.println(str);
    }
}
